from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import Keys
from selenium.webdriver.support.select import Select


class Base():

    #初始化浏览器
    def __init__(self,driver):
        self.driver=driver

    #访问目标网页
    def open_url(self,url):
        self.driver.get(url)

    #封装元素定位的方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,30,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    #封装点击方法
    def click(self,loc):
        self.find_element(loc).click()

    #封装输入方法
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    #封装断言
    def text(self,loc):
        return self.find_element(loc).text

    #封装滚动条
    def scrollTo_X(self,loc,x):
        self.driver.execute_script(f'window scrollTo(0,{x})')

    #封装alert弹窗
    def alert_accept(self):
        alert=self.driver.switch_to.alert.accept()

    #封装iframe切换
    def get_frame(self, loc):
         self.driver.switch_to.frame(self.find_element(loc))

    #封装退出框架的方法
    def frame_content(self):
        self.driver.switch_to.default_content()

    #封装窗口切换
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    #封装鼠标事件
    def action_click(self,loc):
        ActionChains(self.driver).click(self.find_element(loc)).perform()

    #封装键盘事件
    def keys(self,loc):
        self.driver.find_element(loc).send_keys(Keys.ENTER)

    #封装浏览器窗口操作
    def llq(self):
        self.driver.refresh()

    #封装下拉框方法
    def select_index(self,loc,x):
        index=Select(self.find_element(loc))
        index.select_by_index(x)

    def select_value(self,loc,x):
        value=Select(self.find_element(loc))
        value.select_by_value(x)

    def select_text(self,loc,x):
        text=Select(self.find_element(loc))
        text.select_by_visible_text(x)
